//
//  walletViewController.m
//  ioschishan
//
//  Created by 千翼网络科技 on 2017/7/13.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "walletViewController.h"
#import "walletTableViewCell.h"
#import "billViewController.h"

@interface walletViewController ()<UITableViewDelegate , UITableViewDataSource>{
    
    
}

@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,strong) NSMutableDictionary *dataDic;
@property(nonatomic,strong) UITableView *walletTableView;
@property(nonatomic,strong) UILabel *moneyNumLabel;

@end

@implementation walletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self requestData];
}

#pragma mark ----------------------- requestData ---------------------

- (void)requestData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:more_mywallet
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       [hud removeFromSuperview];
                                       
                                       _dataDic = returnData[@"data"];
                                       NSLog(@"\n\n\n===钱包===\n\n\n%@",_dataDic);
                                       [_walletTableView reloadData];
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
}

#pragma mark ------------------ init -------------------

- (void)initView{
    
    self.title = @"我的钱包";
    
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, screenScale(80), screenScale(40))];
    rightBtn.titleLabel.font = get4sFont(14);
    [rightBtn setTitle:@"我的账单" forState:UIControlStateNormal];
    [rightBtn setTitleColor:getColor(@"4a4a4a") forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItme = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItme;
    
    _walletTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), scrHeight() - 64)];
    _walletTableView.backgroundColor = getColor(@"ffffff");
    _walletTableView.delegate = self;
    _walletTableView.dataSource = self;
    _walletTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_walletTableView];
}

#pragma mark ------------------ click -------------------

- (void)rightBtnClick{
    
    billViewController *searchVC = [[billViewController alloc]init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

#pragma mark -------------------- tableView ----------------------

#pragma mark --- UITableViewDelegate ---

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return screenScale(128);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return screenScale(60);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(128))];
    headerView.backgroundColor = getColor(@"3a95ff");
    
    UILabel *numLabel = [ConfigXp labelWithFrame:CGRectZero
                                            text:@"我的金币数量"
                                       textColor:getColor(@"ffffff")
                                            font:get4sFont(13)
                                   textAlignment:NSTextAlignmentLeft
                                 backgroundColor:[UIColor clearColor]];
    [headerView addSubview:numLabel];
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(headerView).with.offset(screenScale(30));
        make.left.mas_equalTo(headerView).with.offset(screenScale(15));
        
        
    }];
    
    _moneyNumLabel = [ConfigXp labelWithFrame:CGRectZero
                                         text:@"521"
                                    textColor:getColor(@"ffffff")
                                         font:get4sFont(36)
                                textAlignment:NSTextAlignmentLeft
                              backgroundColor:[UIColor clearColor]];
    [headerView addSubview:_moneyNumLabel];
    [_moneyNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(headerView).with.offset(screenScale(59));
        make.left.mas_equalTo(headerView).with.offset(screenScale(15));
        
        
    }];
    
    UILabel *messageLabel = [ConfigXp labelWithFrame:CGRectZero
                                                text:@"水滴"
                                           textColor:getColor(@"ffffff")
                                                font:get4sFont(13)
                                       textAlignment:NSTextAlignmentLeft
                                     backgroundColor:[UIColor clearColor]];
    [headerView addSubview:messageLabel];
    [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(headerView).with.offset(screenScale(80));
        make.left.mas_equalTo(_moneyNumLabel.mas_right).with.offset(screenScale(10));
        
        
    }];

    UIView *threadView = [[UIView alloc]init];
    threadView.backgroundColor = getColor(@"f5f5f5");
    [headerView addSubview:threadView];
    [threadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(headerView).with.offset(0);
        make.left.mas_equalTo(headerView).with.offset(0);
        make.width.mas_equalTo(scrWidth());
        make.height.mas_equalTo(screenScale(8));
    }];
    
    _moneyNumLabel.text = _dataDic[@"money"];
    
    return headerView;
}

#pragma mark --- UITableViewDataSource ---

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [_dataDic[@"gold_money"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView registerClass:[walletTableViewCell class] forCellReuseIdentifier:@"identifier"];
    walletTableViewCell *cell = (walletTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"identifier" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (_dataDic) {
        
        cell.nameLable.text = [_dataDic[@"gold_money"][indexPath.row][@"gold_money"] stringByAppendingString:@"水滴"];
        cell.textLable.text = [@"¥" stringByAppendingString:_dataDic[@"gold_money"][indexPath.row][@"money"]];
        
    }
    
    return cell;
}

@end
